//订单分佣流水
import React from 'react'
import { Modal, Form, Input, Button, Space, InputNumber, message, Descriptions, Row, Col } from "antd";
import TheTable from '@/components/TheTable'
import ThePagination from '@/components/ThePagination'
import TheButton from '@/components/TheButton'
import TheQueryForm from '@/components/TheQueryForm';
import HandleDialog from '../components/handleDialog'
import Add from '../components/add'
import { apiGetReportPage ,apiDictionaryList} from '@/request/riskControl'
import enums from "../Enum"
import { getDateTime2 } from '@/utils/getDate'
import moment from 'moment';
import store from "@/store/store"
import _ from 'lodash'

// 获取当前年份和月份
const currentYear = moment().year();
const currentMonth = moment().month();
const currentDate = moment().date();
//处理年月为上一个月
let year = '', month = ''
if (currentMonth === 0) {
    year = currentYear - 1
    month = 12
} else {
    year = currentYear
    month = currentMonth - 1
}
export default class report extends React.Component {
    handleRefs = React.createRef()
    addRefs = React.createRef()
    state = {
        searchOption: [
            { field: 'memberData', label: '举报人ID/昵称', placeholder: '请输入', type: 'Input', defaultValue: '' },
            { field: 'reportMemberData', label: '举报对象ID/昵称', placeholder: '请输入', type: 'Input', defaultValue: '' },
            {
                field: 'time', label: '举报时间', type: 'RangePicker',
                otherConfig: {
                    showTime: true,
                    disabledDate: (current) => {
                        // 获取要选择的年份和月份
                        const selectedYear = current.year();
                        const selectedMonth = current.month();
                        const selectedDate = current.date();
                        // 如果要选择的年份大于当前年份，禁用它
                        if (selectedYear > currentYear) {
                            return true;
                        }
                        // 如果要选择的年份等于当前年份，且要选择的月份大于当前月份，禁用它
                        if (selectedYear === currentYear && selectedMonth > currentMonth) {
                            return true;
                        }
                        // 如果要选择的年份等于当前年份，且要选择的月份大于当前月份，禁用它
                        if (selectedYear === currentYear && selectedMonth === currentMonth && selectedDate > currentDate) {
                            return true;
                        }
                        // 允许选择该日期
                        return false;
                    }
                },
            },
            { field: 'reviewType', label: '举报类型', placeholder: '全部', type: 'Select', options: [{ label: "宠圈评论", value: "MEMBER_CIRCLE_COMMENT" }, { label: "宠圈动态", value: "MEMBER_CIRCLE_DYNAMIC" }, { label: "用户被举报", value: "MEMBER_ACCOUNT" }] },
            { field: 'reviewStatus', label: '状态', placeholder: '全部', type: 'Select', options: [{ label: '全部', value: '' }, { label: '未处理', value: 'N' }, { label: '已处理', value: 'Y' }] },
            { field: 'reviewReason', label: '举报原因', placeholder: '请输入', type: 'Select', defaultValue: '', txt: 'name', value: 'name', api: apiDictionaryList,params:{type:'REASON_FOR_REPORTING'} }
        ],
        columns: [
            {
                title: "id",
                dataIndex: "memberReviewRecordsId",
                key: "memberReviewRecordsId",
            },
            {
                title: "举报人ID",
                dataIndex: "memberId",
                key: "memberId",
                render: (txt, _) => {
                    return txt || '系统'
                }
            },
            {
                title: "举报人昵称",
                dataIndex: "memberName",
                key: "memberName",
                render: (txt, _) => {
                    return txt || '系统'
                }
            },
            {
                title: "举报对象ID",
                dataIndex: "reportMemberId",
                key: "reportMemberId",
            },
            {
                title: "举报对象昵称",
                dataIndex: "reportMemberName",
                key: "reportMemberName",
            },
            {
                title: "举报类型",
                dataIndex: "reviewType",
                key: "reviewType",
                render: (txt, _) => {
                    const { reportState, reportState_content, commonHeaderReportType } = enums
                    // const typeArr = Object.assign({}, reportState, reportState_content)
                    const typeArr = { "MEMBER_CIRCLE_COMMENT": "宠圈评论", "MEMBER_CIRCLE_DYNAMIC": "宠圈动态", "MEMBER_ACCOUNT": "用户被举报","MEMBER_CHAT_PERMISSION":'聊天' }
                    return typeArr[txt]
                }
            },
            {
                title: "举报原因",
                dataIndex: "reviewReason",
                key: "reviewReason",
            },
            {
                title: "举报时间",
                dataIndex: "createTime",
                key: "createTime",
            },
            {
                title: "状态",
                dataIndex: "reviewStatus",
                key: "reviewStatus",
                render: (text) => {
                    let settlementStatus = ''
                    switch (text) {
                        case 'Y':
                            settlementStatus = '已处理'
                            break
                        case 'N':
                            settlementStatus = '未处理'
                            break
                        default:
                            settlementStatus = '-'
                            break
                    }
                    return settlementStatus
                }
            },
            {
                title: '操作',
                align: 'center',
                fixed: 'right',
                width: 200,
                render: record => {
                    return (<>
                        <TheButton haspermi={['riskControl:report:view']} onClick={() => this.goView(record.memberReviewRecordsId, 'report')}>查看</TheButton>
                        {/* {record.reviewStatus == 'N' && <TheButton haspermi={['riskControl:report:dispose']} onClick={() => this.handleRefs.open({ id: record.memberReviewRecordsId, mode: 'F', type: this.getType(record.reviewType) ? "content" : 'user', from: "report" })}>去处理</TheButton>} */}
                        {record.reviewStatus == 'N' && <TheButton haspermi={['riskControl:report:dispose']} onClick={() => this.goView(record.memberReviewRecordsId, 'reportDoing')}>去处理</TheButton>}

                    </>)
                }
            }
        ],
        dataSource: [],
        pageData: { pageNo: 1, pageSize: 10 },
        total: 0,
        exportBtnLoading: false,
    }
    componentDidMount() {
        const { searchOption } = this.state
        const { reportState, reportState_content } = enums
        const typeArr = Object.assign({}, reportState, reportState_content)
        // searchOption.map(item => {
        //     if (item.field == 'reviewType') {
        //         item.options = Object.keys(typeArr).map(obj => {
        //             return {
        //                 label: typeArr[obj],
        //                 value: obj
        //             }
        //         })
        //     }
        //     return item
        // })
        let searchParams = store.getState().searchValue
        let { pageNo, pageSize, queryForm, pathName } = searchParams
        if (searchParams && pageNo && pathName == 'report') {
            this.setState({
                queryForm, pageData: { pageNo, pageSize }
            }, () => {
                this.queryFormRef.onSetDefault({ ...queryForm })
                this.getData({ ...queryForm })
            })
        } else {
            this.getData()
        }
        this.setState({ searchOption })
    }
    getType = (val) => {
        const { reportState_content } = enums
        return ["MEMBER_CIRCLE_COMMENT", "MEMBER_CIRCLE_DYNAMIC"].includes(val)
    }
    goView = (id, way) => {
        this.props.history.push(`/riskControl/${way}/view/${id}`)
    }

    //数据处理
    getData = (values) => {
        let { pageData, queryForm } = this.state
        this.setState({
            queryForm: values === undefined ? this.state.queryForm : { ...values }
        }, async () => {
            let params = { ...pageData, queryForm }
            store.dispatch({
                type: 'SaveSearchValues',
                data: { pathName: 'OrderCommission', ...params }
            })
            await apiGetReportPage({ ...pageData, ...this.state.queryForm }).then(res => {
                if (res.code === 0) {
                    this.setState({
                        dataSource: res.data,
                        pageData: {
                            pageNo: res.pageNo,
                            pageSize: res.pageSize
                        },
                        total: res.totalCount
                    })
                }
            })
        })
    }
    //分页
    pageChange = (pageNo, pageSize) => {
        this.setState({ pageData: { pageNo, pageSize } }, () => {
            this.getData(this.state.queryForm)
        })
    }
    //搜索
    onFormSearch(values) {
        if (values && !_.isEmpty(values.time)) {
            values.createStartTime = getDateTime2(values.time[0])
            values.createEndTime = getDateTime2(values.time[1])
            delete values.time
        }
        if (values && !_.isEmpty(values.address)) {
            values.address = values.address.join('')
        }
        this.setState({
            pageData: { pageNo: 1, pageSize: 10 }, year, month
        }, () => {
            this.getData(values)
        })
    }
    //搜索重置
    onReset() {
        this.setState({ queryForm: {} }, () => {
            this.pageChange(1, 10)
        })
    }
    // 导出
    export = () => {

    }
    add = () => {

    }

    render() {
        const { searchOption, columns, dataSource, pageData: { pageNo, pageSize }, total, exportBtnLoading } = this.state
        return (
            <div className='p-layout-box commodity-content'>
                <TheQueryForm
                    ref={(ref) => this.queryFormRef = ref}
                    option={searchOption}
                    show={true}
                    onReset={() => this.onReset()}
                    onGetValues={(values) => this.onFormSearch(values)} >
                </TheQueryForm>
                <div style={{ display: 'flex', justifyContent: 'flex-end', marginBottom: 20 }}>
                    <TheButton haspermi={['riskControl:report:add']} size='middle' type='default' onClick={() => { this.addRefs.open() }} loading={exportBtnLoading}>新增</TheButton>
                </div>
                {/* <div style={{ display: 'flex', justifyContent: 'flex-end', marginBottom: 20 }}>
                    <TheButton haspermi={['distribution:orderCommission:export']} size='middle' type='default' onClick={() => { this.export() }} loading={exportBtnLoading}>导出</TheButton>
                </div> */}
                <TheTable
                    scroll={{ x: "max-content", y: '43vh' }}
                    columns={columns}
                    dataSource={dataSource}
                    rowKey={'userId'}
                />
                <ThePagination pageNo={pageNo} pageSize={pageSize} total={total} pageChange={this.pageChange} />
                <HandleDialog ref={ref => this.handleRefs = ref} getData={this.getData}></HandleDialog>
                <Add ref={ref => this.addRefs = ref} getData={this.getData}></Add>
            </div>
        )
    }
}
